package task

import (
	"fmt"
	"strconv"

	"github.com/xuelang-group/sh-solver-scheduler/internal/domain/entity"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// 求解器 service
func GetSolverService(task *entity.SolverTask) (*corev1.Service, error) {
	labels := GetDefaultLabels(task.ID)

	service := corev1.Service{
		ObjectMeta: metav1.ObjectMeta{
			Name:      fmt.Sprintf("solver-%v", task.ID),
			Namespace: task.Namespace,
			Labels:    labels,
		},
		Spec: corev1.ServiceSpec{
			Selector: labels,
			Type:     corev1.ServiceTypeClusterIP,
			Ports: []corev1.ServicePort{
				{
					Name:     "http",
					Port:     9000,
					Protocol: corev1.ProtocolTCP,
				},
			},
		},
	}

	return &service, nil
}

func GetDefaultLabels(taskID int) map[string]string {
	labels := map[string]string{
		"whichis":      "solverTask",
		"solverTaskID": strconv.Itoa(taskID),
	}

	return labels
}
